package com.dldata.devtools30.utils;

/**

 */


import net.sf.json.JSONSerializer;
import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.util.HashMap;
import java.util.Map;


public class AesUtil {

    /**
     * 产生key的主体名称
     * 参考命名格式xxx@xx.xxx其他命名格式参考Java Cryptography Architecture API Specification & Reference 中的附录 A
     */
    private IvParameterSpec dps = new IvParameterSpec("1234567812345678".getBytes());
    private SecretKeySpec secretKey = new SecretKeySpec("1234567812345678".getBytes(), "AES");
    
    public AesUtil(){
    
    }

    public static AesUtil getAesUtil(String keyPassword, String ivparameterspec){
        AesUtil aesUtil = new AesUtil();
        aesUtil.secretKey = new SecretKeySpec(MD5Util.MD5Encoder(keyPassword).substring(0,16).getBytes(), "AES");
        aesUtil.dps = new IvParameterSpec(MD5Util.MD5Encoder(ivparameterspec).substring(0,16).getBytes());
        return aesUtil;
    }
    
    /**
     * DES加密字符串，返回加密后的字符串
     *
     * @param encryptString
     *            要加密的字符串
     * @return 返回base64转码后的字符串
     * @throws java.security.InvalidKeyException
     * @throws java.security.NoSuchAlgorithmException
     * @throws javax.crypto.NoSuchPaddingException
     * @throws java.io.UnsupportedEncodingException
     * @throws javax.crypto.IllegalBlockSizeException
     * @throws javax.crypto.BadPaddingException
     */
    public  String encrypt(String encryptString) {
        try {
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            byte[] by = encryptString.getBytes("utf8");
            cipher.init(Cipher.ENCRYPT_MODE, secretKey,dps);
            byte[] cipherText = cipher.doFinal(by);
            return Base64.encodeBase64URLSafeString(cipherText);
        } catch (Exception e) {
            //Log.error(e);
            e.printStackTrace();
            return encryptString;
        }
    }

    /**
     * DES解密字符串
     *
     * @param
     * @return
     * @throws java.security.NoSuchAlgorithmException
     * @throws javax.crypto.NoSuchPaddingException
     * @throws java.io.UnsupportedEncodingException
     * @throws java.security.InvalidKeyException
     * @throws javax.crypto.IllegalBlockSizeException
     * @throws javax.crypto.BadPaddingException
     */
    public  String decrypt(String decryptString) {
        try {
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            byte[] by = Base64.decodeBase64(decryptString);
            cipher.init(Cipher.DECRYPT_MODE, secretKey,dps);
            byte[] cipherText = cipher.doFinal(by);
            return new String(cipherText, "utf-8");
        } catch (Exception e) {
            return decryptString;
        }
    }

    public static void main(String args[]) {
        try {
            Map<String,Object> token = new HashMap<>();
            token.put("st",System.currentTimeMillis()/1000);
            token.put("et",System.currentTimeMillis()/1000);
            token.put("uid", MD5Util.MD5Encoder("1"));
            token.put("unm", MD5Util.MD5Encoder("1"));
            token.put("cid", MD5Util.MD5Encoder("1"));

            String s = JSONSerializer.toJSON(token).toString();

            AesUtil desutil= AesUtil.getAesUtil(Public.TOKEN_PRINCIPALNAME, Public.TOKEN_KEYPASSWORD);

            // 需要加密的字符串
            String str =s;// "yuchen352416";
            System.out.println(str);
            // 加密后的字符串
            String eStr = desutil.encrypt(str);
            // 解密已经加密的字符串
            String dStr = desutil.decrypt(eStr);
            System.out.println(eStr);
            System.out.println(dStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
